package com.hj.chartdemo.chart;


import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import java.util.ArrayList;

public class LineChartManagger {

  private static LineChartManagger instance = null;

  protected Typeface tfRegular;
  protected Typeface tfLight;

  private Context activity;

  public static LineChartManagger getInstance() {
    if (instance == null) {
      instance = new LineChartManagger();
    }
    return instance;
  }

  // 初始化
  public void init(Context context, LineChart lineChart) {
    instance.activity = context;

    tfRegular = Typeface.createFromAsset(activity.getAssets(), "OpenSans-Regular.ttf");
    tfLight = Typeface.createFromAsset(activity.getAssets(), "OpenSans-Light.ttf");

    lineChart.getDescription().setEnabled(false);
    lineChart.setDrawGridBackground(false);
    lineChart.setDragEnabled(true);
    lineChart.setExtraBottomOffset(10f);

    int ratio = 100 / 6;
    // 显示的时候是按照多大的比率缩放显示,1f表示不放大缩小
    lineChart.zoom(1f, 1f, 0f, 0f);
    lineChart.setScaleEnabled(true);

    XAxis lineXAxis = lineChart.getXAxis();
    lineXAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    lineXAxis.setDrawGridLines(false);
    lineXAxis.setGranularity(1f);
    lineXAxis.setDrawAxisLine(true);
    lineXAxis.setAxisMinimum(0.5f);

    YAxis lineLeftAxis = lineChart.getAxisLeft();
    lineLeftAxis.setTypeface(tfRegular);
    lineLeftAxis.setGranularity(1f);
    lineLeftAxis.setAxisMinimum(0f);
    YAxis lineRightAxis = lineChart.getAxisRight();
    lineRightAxis.setEnabled(false);

    Legend lineLegend = lineChart.getLegend();
    lineLegend.setForm(Legend.LegendForm.SQUARE);
    // 设置图形与文本的间隔（单位：dp），默认5dp
    lineLegend.setFormToTextSpace(5f);
    // 表头线条放置的位置
    lineLegend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
    lineLegend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
    // 表头多条线条的排列方式
    lineLegend.setOrientation(Legend.LegendOrientation.VERTICAL);
    lineLegend.setDrawInside(false);
    lineLegend.setForm(Legend.LegendForm.SQUARE);
    lineLegend.setFormSize(9f);
    lineLegend.setTextSize(11f);
    // 表头间距
    lineLegend.setXEntrySpace(10f);
    // 表头行间距
    lineLegend.setYEntrySpace(5f);
    // 自动换行
    lineLegend.setWordWrapEnabled(true);

    lineChart.setNoDataText("暂无数据");
    lineChart.animateX(750);
  }

  public void loadData(
      Context context,
      LineChart lineChart,
      ArrayList<ArrayList<Entry>> datas,
      ArrayList<String> titles,
      ArrayList<Integer> colors,
      ArrayList<String> xlab,
      Boolean showLegend) {
    loadData(context, lineChart, datas, titles, colors, xlab, showLegend, "");
  }

  public void loadData(
      Context context,
      LineChart lineChart,
      ArrayList<ArrayList<Entry>> datas,
      ArrayList<String> titles,
      ArrayList<Integer> colors,
      final ArrayList<String> xlab,
      Boolean showLegend,
      String ext) {
    lineChart.getLegend().setEnabled(showLegend);

    if (lineChart.getData() != null && lineChart.getData().getDataSetCount() > 0) {
      int count = lineChart.getData().getDataSetCount();
      for (int i = 0; i < count; i++) {
        if (datas.size() - 1 >= i) {
          LineDataSet lineDataSet = (LineDataSet) lineChart.getData().getDataSetByIndex(i);
          lineDataSet.setValues(datas.get(i));
        }
      }
      lineChart.getData().notifyDataChanged();
      lineChart.notifyDataSetChanged();
    } else {
      ArrayList<ILineDataSet> sets = new ArrayList<>();
      for (int i = 0; i < datas.size(); i++) {
        String title = "";
        if (titles.size() - 1 >= i) {
          title = titles.get(i);
        }
        LineDataSet d1 = new LineDataSet(datas.get(i), title);
        d1.setLineWidth(2.5f);
        d1.setCubicIntensity(0.5f);
        d1.setCircleRadius(4.5f);
        d1.setHighLightColor(Color.rgb(248, 181, 81));
        d1.setDrawValues(false);
        if (colors.size() - 1 >= i) {
          d1.setColor(colors.get(i));
          d1.setCircleColor(colors.get(i));
        }
        sets.add(d1);
      }
      lineChart.setData(new LineData(sets));
    }
    if (xlab != null) {
      XAxis lineXAxis = lineChart.getXAxis();
      lineXAxis.setValueFormatter(
          new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
              Log.e("aaaaaaa", value + "");
              if (value > 0) {
                return xlab.size() > value ? xlab.get((int) value - 1) : "";
              }return "";
            }
          });
      if (datas.size() > 0) {
        lineXAxis.setLabelCount(datas.get(0).size());
        if (datas.get(0).size() > 10) {
          lineXAxis.setSpaceMin(10f);
          lineXAxis.setLabelRotationAngle(20f);
          lineXAxis.resetAxisMaximum();
          lineChart.setScaleMinima(3f, 1f);
        }
      } else lineXAxis.setLabelCount(6,false);

//      lineChart.getAxisLeft().resetAxisMaximum();

      // X轴的设置及自定义X轴
      BarAxisValueFormatter xAxisFormatter = new BarAxisValueFormatter(lineChart, xlab);
      XYMarkerView mv = new XYMarkerView(context, xAxisFormatter, ext);
      mv.setChartView(lineChart); // For bounds control
      lineChart.setMarker(mv); // Set the marker to the chart
    }
    lineChart.invalidate();
  }
}
